package io.github.source.media.bean;

import android.text.TextUtils;
import android.util.Log;

import com.github.catvod.utils.Json;
import com.google.gson.JsonElement;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;

import org.json.JSONObject;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Path;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import io.github.source.Init;
import io.github.source.bean.Flag;
import io.github.source.bean.Url;
import io.github.source.gson.UrlAdapter;

/**
 * @ClassName: MediaPlay
 * @Description: TODO(播放)播放信息（Todo 精简对外部的依赖）
 * @author: huangrongcai
 * @date: 2024/6/3
 * @Copyright: 黄嵘才（HuangRc)
 */
public class MediaPlay implements Serializable {

    /*
    {
	"limit": 0,
	"total": 0,
	"parse": 0,
	"page": 0,
	"jx": 0,
	"subs": [],
	"pagecount": 0,
	"format": "application\/octet-stream",
	"header": "{\"User-Agent\":\"Mozilla\/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit\/537.36 (KHTML, like Gecko) quark-cloud-drive\/3.0.1 Chrome\/100.0.4896.160 Electron\/18.3.5.12-a038f7b798 Safari\/537.36 Channel\/pckk_other_ch\",\"Referer\":\"https:\/\/pan.quark.cn\/\"}",
	"url": "http:\/\/127.0.0.1:-1\/proxy?do=quark&type=proxyQuark&shareFile_id=a35ccbf4e1c74495949136fcc644b56f&fid_token=a493448cceaa6e122cd20651001140ad&flag=原画&name=\/职业杀手.Hit Man.2024.1080p中英字幕.mp4",
	"danmaku": "http:\/\/127.0.0.1:-1\/proxy?do=woggDanMu"
    }
    * */
    /*
    {
	"code": 200,
	"playVodId": 72454,
	"ip": "121.62.61.51",
	"playSourceId": 119,
	"url": "http://npy.hzhcbkj.cn/2025-07-03/119_72454_NBY-9c190066de24bfdcf7e6330ce43dca00.m3u8?sign=1751541164-0-0-92acc7fbd5b86cfc866dd86dbcd4510b",
	"From": null,
	"From_Url": "NBY-9c190066de24bfdcf7e6330ce43dca00",
	"adv": "1QQ群已经被盯上,问题联系TG：https://t.me/+Iso8bME4Q25kMWE1",
	"UA": "okhttp-okgo/jeasonlzy",
	"type": "hls"
}
    * */
    /*
    {
	"jx": 0,
	"url": [
		"清晰 480P",
		"http://127.0.0.1:-1/proxy?do=bili&aid=114771743475867&cid=30775248090&qn=32&type=mpd",
		"流畅 360P",
		"http://127.0.0.1:-1/proxy?do=bili&aid=114771743475867&cid=30775248090&qn=16&type=mpd"
	],
	"danmaku": "https://api.bilibili.com/x/v1/dm/list.so?oid=30775248090",
	"parse": 0,
	"header": "{\"cookie\":\"buvid3\\u003d84B0395D-C9F2-C490-E92E-A09AB48FE26E71636infoc\",\"User-Agent\":\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36\",\"Referer\":\"https://www.bilibili.com\"}",
	"format": "application/dash+xml"
}
    * */

    /*播放地址*/
    @SerializedName("url")
    @JsonAdapter(UrlAdapter.class)
    private Url url;

    /**
     * 头部数据
     */
    @SerializedName("header")
    private JsonElement header;
    /**
     * 头部数据
     */
    @SerializedName("header2")
    private Map<String, String> header2;

    private String playUrl;

    /**
     * 源站
     */
    @SerializedName("key")
    private String key;

    @SerializedName("parse")
    private Integer parse;

    @SerializedName("jx")
    private Integer jx;
    @SerializedName("flag")
    private String flag;

    @SerializedName("jxFrom")
    private String jxFrom;
    @SerializedName("click")
    private String click;
    @SerializedName("danmaku")
    private String danmaku;
    @SerializedName("format")
    private String format;

    /** SYS = 0; IJK = 1; EXO = 2;*/
    private int playerType;

    /**
     * 播放的名称
     */
    private String playName;
    /**
     * 播放的标签
     */
    private int playFlag;
    /**
     * 播放的选项
     */
    private int playIndex;
    @SerializedName("realUrl")
    public String realUrl;


    /**
     * 直实的播放地址
     *
     * @return
     */
    public String getRealUri() {
        return realUrl;
    }

    public String getClick() {
        return TextUtils.isEmpty(click) ? "" : click;
    }

    public static MediaPlay objectFrom(JsonElement element) {
        try {
            return Init.json().fromJson(element, MediaPlay.class);
        } catch (Exception e) {
            Log.d("TAG", "初始化json MediaPlay " + e);
            e.printStackTrace();
            return new MediaPlay();
        }
    }

    public static MediaPlay fromObject(JSONObject object) {
        return object == null ? empty() : objectFrom(object.toString());
    }

    public static MediaPlay empty() {
        return new MediaPlay();
    }

    public static MediaPlay objectFrom(String element) {
        try {
            return Init.json().fromJson(element, MediaPlay.class);
        } catch (Exception e) {
            Log.d("TAG", "初始化json MediaPlay " + e);
            e.printStackTrace();
            return new MediaPlay();
        }
    }

    public Url getUrl() {
        return url;
    }

    public void setUrl(Url url) {
        this.url = url;
    }

    public Map<String, String> getHeaders() {
        return Json.toMap(getHeader());
    }

    public JsonElement getHeader() {
        return header;
    }

    public void setHeader(JsonElement header) {
        this.header = header;
    }

    public void setHeader(Map<String, String> header) {
        this.header2 = header;
    }

    public void setHeaders(MediaPlay o, JsonElement header) {
        if (o.getHeader() == null) o.header = header;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Integer getParse() {
        return parse;
    }

    public Integer getParse(int def) {
        return parse == null ? def : parse;
    }

    public Integer getJx() {
        return jx == null ? 0 : jx;
    }

    public void setJx(Integer jx) {
        this.jx = jx;
    }

    public String getJxFrom() {
        return jxFrom;
    }

    public void setJxFrom(String jxFrom) {
        this.jxFrom = jxFrom;
    }

    public void setParse(Integer parse) {
        this.parse = parse;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public String getPlayUrl() {
        return TextUtils.isEmpty(playUrl) ? "" : playUrl;
    }

    public void setPlayUrl(String playUrl) {
        this.playUrl = playUrl;
    }

    @Override
    public String toString() {
        return "MediaPlay{" +
                "url=" + url +
                ", header=" + header +
                ", header2=" + header2 +
                ", playUrl='" + playUrl + '\'' +
                ", key='" + key + '\'' +
                ", parse=" + parse +
                ", jx=" + jx +
                ", flag='" + flag + '\'' +
                ", jxFrom='" + jxFrom + '\'' +
                ", click='" + click + '\'' +
                ", danmaku='" + danmaku + '\'' +
                ", format='" + format + '\'' +
                ", playName='" + playName + '\'' +
                ", playFlag=" + playFlag +
                ", playIndex=" + playIndex +
                ", realUrl='" + realUrl + '\'' +
                '}';
    }
}
